/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpNameDict;
import com.jims.master.modules.csm.service.ExpClassDictService;
import com.jims.master.modules.csm.service.ExpNameDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 消耗品名称字典Controller
 * @author 朱齐
 * @version 2017-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expNameDict")
public class ExpNameDictController extends BaseController {

	@Autowired
	private ExpNameDictService expNameDictService;
    @Autowired
    private ExpClassDictService expClassDictService;

	@ModelAttribute
	public ExpNameDict get(@RequestParam(required=false) String id) {
		ExpNameDict entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expNameDictService.get(id);
		}
		if (entity == null){
			entity = new ExpNameDict();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ExpNameDict expNameDict, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpNameDict> page = expNameDictService.findPage(new Page<ExpNameDict>(request, response), expNameDict);
		model.addAttribute("page", page);
		return "modules/csm/expNameDictIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"query", ""})
	public String querylist(ExpNameDict expNameDict, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpNameDict> page = expNameDictService.findPage(new Page<ExpNameDict>(request, response), expNameDict);
		model.addAttribute("page", page);
		return "modules/csm/expInfoQueryIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ExpNameDict expNameDict, Model model) {
		model.addAttribute("expNameDict", expNameDict);
		return "modules/csm/expNameDictForm";
	}
	@RequiresPermissions("user")

	@RequestMapping(value = {"info", ""})
	public String info(ExpNameDict expNameDict, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpNameDict> page = expNameDictService.findPageByDict(new Page<ExpNameDict>(request, response), expNameDict);
		model.addAttribute("page", page);
		return "modules/csm/expInfo";
	}
    /*
    * @zhuq
    * 保存exp_name_dict
    * */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExpNameDict expNameDict, Model model) {
	    StringData data=new StringData();

		int i=expNameDictService.saveExpName(expNameDict);
        if(i>0){
            data.setCode("success");
            data.setData("保存消耗品名称字典成功");
        }else{
            data.setCode("error");
            data.setData("保存消耗品名称字典失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExpNameDict expNameDict) {
	    StringData data=new StringData();
		int i=expNameDictService.delete(expNameDict);
        if(i>0){
            data.setCode("success");
            data.setData("删除消耗品名称字典成功");
        }else{
            data.setCode("error");
            data.setData("删除消耗品名称字典失败");
        }
		return data;
	}

}